package gxnu.edu.cn.controller;

import gxnu.edu.cn.entity.Ccard;
import gxnu.edu.cn.service.CcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/ccard")
public class CcardController {

    @Autowired
    private CcardService ccardService;

    @GetMapping("/export")
    public String exportCcards(@RequestParam String fileName, @RequestParam String filePath, @RequestParam String format) {
        try {
            List<Ccard> cardList = ccardService.getAllCcards();
            ccardService.exportCcards(fileName, filePath, format, cardList);
            return "导出成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "导出失败: " + e.getMessage();
        }
    }

    @GetMapping("/import")
    public String importCcards(@RequestParam String filePath) {
        try {
            ccardService.importCcards(filePath);
            return "导入成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "导入失败: " + e.getMessage();
        }
    }
}
